package com.telcordia.cvas.rpn;

import static junit.framework.Assert.assertEquals;

import org.junit.Test;

public class PrimeFactorsTest {

	private int value;
	private OperandStack stack;

	@Test
	public void primeFactorOf2Is2() {
		givenOperand(2);
		whenCalculatingPrimeFactors();
		thenTheResultShouldInclude(2);
	}

	@Test
	public void primeFactorOf3is3() {
		givenOperand(3);
		whenCalculatingPrimeFactors();
		thenTheResultShouldInclude(3);
	}

	@Test
	public void primeFactorsOf4Are2And2() {
		givenOperand(4);
		whenCalculatingPrimeFactors();
		thenTheResultShouldInclude(2);
		and(2);
	}

	@Test
	public void primeFactorsOf6Are3And2() {
		givenOperand(6);
		whenCalculatingPrimeFactors();
		thenTheResultShouldInclude(3);
		and(2);
	}

	@Test
	public void primeFactorsOf8Are2And2And2() {
		givenOperand(8);
		whenCalculatingPrimeFactors();
		thenTheResultShouldInclude(2, 2, 2);
	}

	private void and(int expected) {
		thenTheResultShouldInclude(expected);
	}

	private void thenTheResultShouldInclude(Integer... expected) {
		for (int i = 0; i < expected.length; ++i)
			assertEquals(expected[i], (Integer)stack.pop());
	}

	private void whenCalculatingPrimeFactors() {
		stack = new OperandStack();
		stack.push(value);
		new PrimeFactors().execute(stack);
	}

	private void givenOperand(int value) {
		this.value = value;
	}

}
